package com.nineclock.attendance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nineclock.attendance.dto.AttendSettingDTO;
import com.nineclock.attendance.mapper.AttendSettingMapper;
import com.nineclock.attendance.pojo.AttendSetting;
import com.nineclock.attendance.service.AttendSettingService;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.filter.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
@Transactional
public class AttendSettingServiceImpl implements AttendSettingService {

    @Autowired(required = false)
    private AttendSettingMapper settingMapper;

    @Override
    public PageResult<AttendSettingDTO> querySetting(Integer page, Integer pageSize) {
        IPage<AttendSetting> ipage = new Page<>(page,pageSize);
        LambdaQueryWrapper<AttendSetting> warpper = new LambdaQueryWrapper<>();
        warpper.eq(AttendSetting::getCompanyId, CurrentUserHolder.get().getCompanyId());
        ipage = settingMapper.selectPage(ipage, warpper);
        List<AttendSetting> settingList = ipage.getRecords();
        List<AttendSettingDTO> attendSettingDTOS = BeanHelper.copyWithCollection(settingList, AttendSettingDTO.class);
        return new PageResult<>(ipage.getTotal(),ipage.getPages(),attendSettingDTOS);

    }

    /**
     * 接口名称：考勤: 补卡规则
     * 接口路径：Put/atte/rule/makeup/{id}
     * @param map
     */
    @Override
    public void updateSetting(Map<String, Object> map) {
        AttendSetting attendSetting = new AttendSetting();
        attendSetting.setId(Long.valueOf(map.get("id").toString()));
        attendSetting.setAllowMakeup(Boolean.valueOf(map.get("allowMakeup").toString())?1:0);
        attendSetting.setMaxMakeupNumber(map.get("maxMakeupNumber").toString());
        attendSetting.setCurrentMonthOnly(Boolean.valueOf(map.get("currentMonthOnly").toString())?1:0);
        settingMapper.updateById(attendSetting);
    }


}
